<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标准名称" prop="standardName">
        <el-input v-model="queryParams.standardName" placeholder="请输入标准名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="图幅" prop="sheetSize">
        <el-input v-model="queryParams.sheetSize" placeholder="请输入图幅" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="组件名称" prop="componetName">
        <el-input v-model="queryParams.componetName" placeholder="请输入组件名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['faw:standard:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['faw:standard:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['faw:standard:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['faw:standard:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="standardList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="标准名称" align="center" prop="standardName" />
      <el-table-column label="图幅" align="center" prop="sheetSize" />
      <el-table-column label="组件名称" align="center" prop="componetName" />
      <el-table-column label="组件文件" align="center" prop="componetFile" />
      <el-table-column label="xml文件" align="center" prop="xmlFile" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['faw:standard:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['faw:standard:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="standardRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标准名称" prop="standardName">
          <el-input v-model="form.standardName" placeholder="请输入标准名称" />
        </el-form-item>
        <el-form-item label="图幅" prop="sheetSize">
          <el-input v-model="form.sheetSize" placeholder="请输入图幅" />
        </el-form-item>
        <el-form-item label="组件名称" prop="componetName">
          <el-input v-model="form.componetName" placeholder="请输入组件名称" />
        </el-form-item>
        <el-form-item label="组件文件" prop="componetFile">
          <el-text style="color: red;">上传文件后，文件名称将变为文件+时间戳格式显示</el-text>
          <file-upload v-model="form.componetFile" :limit="1" :uploadUrl="'/common/upload/standard/componetFile  '"
            :afferentData="form" />
        </el-form-item>
        <el-form-item label="xml文件" prop="xmlFile">
          <!-- <el-text style="color: red;">上传文件后，文件名称将变为文件+时间戳格式显示</el-text> -->
          <file-upload v-model="form.xmlFile" :limit="1" :uploadUrl="'/common/upload/standard/xmlFile   '"
            :afferentData="form" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Standard">
import { listStandard, getStandard, delStandard, addStandard, updateStandard } from "@/api/flow/standard";
import { ElMessage } from 'element-plus'
const { proxy } = getCurrentInstance();

const standardList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    standardName: null,
    sheetSize: null,
    componetName: null,
    componetFile: null
  },
  rules: {
    standardName: [
      { required: true, message: "标准名称不能为空", trigger: "blur" }
    ],
    sheetSize: [
      { required: true, message: "图幅不能为空", trigger: "blur" }
    ],
    componetName: [
      { required: true, message: "组件名称不能为空", trigger: "blur" }
    ],
    componetFile: [
      { required: true, message: "组件文件不能为空", trigger: "blur" }
    ],
    xmlFile: [
      { required: true, message: "xml文件不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询【请填写功能名称】列表 */
function getList() {
  loading.value = true;
  listStandard(queryParams.value).then(response => {
    standardList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: '',
    standardName: null,
    sheetSize: null,
    componetName: null,
    componetFile: null
  };
  proxy.resetForm("standardRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加标准";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getStandard(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改标准";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["standardRef"].validate(valid => {
    if (valid) {
      const { sheetSize, componetName } = form.value
      if (!areCountsEqual(sheetSize, componetName)) {
        ElMessage.error('图幅和组件名称数量需要保持一致')
        return
      }

      if (form.value.id != null && form.value.id != '') {
        updateStandard(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addStandard(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

function areCountsEqual(sheetSize, componetName) {
  // 使用 split 方法将字符串按逗号分割成数组
  const sheetSizeArray = sheetSize.split(",");
  const componetNameArray = componetName.split(",");

  // 比较两个数组的长度
  return sheetSizeArray.length === componetNameArray.length;
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除该数据项？').then(function () {
    return delStandard(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('faw/standard/export', {
    ...queryParams.value
  }, `standard_${new Date().getTime()}.xlsx`)
}

getList();
</script>
